package com.chapter16;

import java.util.*;

public class MatchedParen {
	
	public void getMatchedParen(List<String> l,StringBuilder sb,int k,int curCount,int open,int close){
		if(curCount==k){
			l.add(sb.toString());
			return;
		}
		if(open<k){
			sb.append("(");
			getMatchedParen(l,sb,k,curCount,open+1,close);
			sb.deleteCharAt(sb.length()-1);
		}
		if(close<open){
			sb.append(")");
			getMatchedParen(l,sb,k,curCount+1,open,close+1);
			sb.deleteCharAt(sb.length()-1);
		}
	}
	
	public void testgetMatchedParen(){
		int k=3;
		StringBuilder sb=new StringBuilder(2*k);
		List<String> l=new ArrayList<String>();
		getMatchedParen(l,sb,k,0,0,0);
		System.out.println(l);
	}
}
